/*
 * @Descripttion: 
 * @version: 
 * @Author: hzj-1510017964@qq.com
 * @Date: 2020-04-28 10:50:06
 * @LastEditors: hzj-1510017964@qq.com
 * @LastEditTime: 2020-04-30 18:00:08
 */

const Sequelize = require('sequelize');


var moment = require('moment');

const db = require('../config/dbconfig')

const UserLoginDao = db.define('user', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    uuid: {
        type: Sequelize.STRING
    },
    number: {
        type: Sequelize.STRING
    },
    quota:{
        type:Sequelize.INTEGER
    }
})

const OutAndInTypeDao = db.define('out_in_type', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    name: {
        type: Sequelize.STRING
    },
    icon: {
        type: Sequelize.STRING
    },
    type: {
        type: Sequelize.INTEGER
    },
})



const UserRecordDao = db.define('user_record', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true
    },
    userId: {
        type: Sequelize.INTEGER,
    },
    typeId: {
        type: Sequelize.INTEGER
    },
    money: {
        type: Sequelize.FLOAT
    },
    outAndIn: {
        type: Sequelize.INTEGER
    },
    clock: {
        type: Sequelize.DATE
    },
    addTime: {
        type: Sequelize.DATE,
    },
    state: {
        type: Sequelize.INTEGER
    },
    remarks: {
        type: Sequelize.STRING
    },
    yearAndMonth: {
        type: Sequelize.STRING
    }
})




//查询uuid
queryUuid = (param) => {
    return UserLoginDao.findOne({
        where: param
    })
}

//将用户数据存入
setUuid = (param) => {
    return UserLoginDao.create(param)
}


//查询类型
getOutAndInTypeDao = (param) => {
    console.log(param)
    if (param.type) {
        return OutAndInTypeDao.findAll({
            where: param
        })
    } else {
        return OutAndInTypeDao.findAll()
    }

}


//添加记录
addUserRecordDao = (param) => {
    return UserRecordDao.create(param)
}








getUserRecordDao = (param) => {
    UserRecordDao.belongsTo(OutAndInTypeDao, { foreignKey: 'typeId', targetKey: 'id' });
    return UserRecordDao.findAll({
        where: param,
        include: [{ // include关键字表示关联查询
            model: OutAndInTypeDao, // 指定关联的model
            attributes: ['name', 'icon'], // 这里的attributes属性表示查询class表的name和rank字段，其中对name字段起了别名className
        }],
    })
}



totalDao = (param) => {
   UserRecordDao.belongsTo(OutAndInTypeDao, {foreignKey: 'typeId', targetKey: 'id'});
   return UserRecordDao.findAll({
        attributes:['typeId', [Sequelize.fn('SUM', Sequelize.col('money')), 'money']],
        where:param,
        group: 'typeId',
        include: [{ 
            model: OutAndInTypeDao, 
            attributes: ['name','icon'], 
        }],
    })
}


setQuotaDao = (updateData,param) =>{
    return UserLoginDao.update(updateData,{
        where:param
    })
}


getUserInfo = (param) =>{
    return UserLoginDao.findOne({
        where:param
    })
}








module.exports = {
    queryUuid,
    setUuid,
    getOutAndInTypeDao,
    addUserRecordDao,
    getUserRecordDao,
    totalDao,
    setQuotaDao,
    getUserInfo
}